<?php

/**
 * 控制器基类
 * 
 * 该类继承自Yaf\Controller_Abstract,可以使用读取或使用Yaf\Controller_Abstract的属性和方法
 */
class Controller extends Yaf\Controller_Abstract {

    /**
     * 返回码,默认为null,当设置了该值后将输出json数据
     * @var int 
     */
    protected $code = null;

    /**
     * 返回内容,默认为null,当设置了该值后将输出json数据
     * @var mixed 
     */
    protected $content = null;

    /**
     * 属性读取魔术方法
     * 
     * 当读取一个不存在的属性时,如果该属性在注册空间中有存在则读取注册空间的值
     * @param string $name 属性值
     * @return \$name
     */
    public function __get($name) {
        if ($name == 'view')
            return $this->_view;
        if (Yaf\Registry::has($name)) {
            return Yaf\Registry::get($name);
        }
    }

    /**
     * 初始化
     */
    public function init(){
        defined('NOW_TIME')  or define('NOW_TIME',      $_SERVER['REQUEST_TIME']);
        defined('IS_GET')    or define('IS_GET',        $this->_request->isGet() );
        defined('IS_POST')   or define('IS_POST',       $this->_request->isPost() );
        defined('IS_AJAX')   or define('IS_AJAX',       $this->_request->isXmlHttpRequest() );
        defined('HTTP_REFERER') or define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
        defined('MODULE_NAME')    or define('MODULE_NAME',     $this->_request->module);
        defined('CONTROLLER_NAME')    or define('CONTROLLER_NAME',     $this->_request->controller);
        defined('ACTION_NAME')    or define('ACTION_NAME',     $this->_request->action);
    }

    //变量注入
    public function assign($name,$value){
        $this->_view->assign($name,$value);
    }

    //显示
    public function show($tpl,$param=''){
        $this->_view->display($tpl,$param);
    }


    public function message($message, $url = '', $time = 3) {
        $this->assign('message',$message);
        $this->assign('waitSecond',$time);
        $this->assign('url',$url?$url:HTTP_REFERER);
        $this->show('Error/message.php');
        return false;
    }

    public function error($message, $url = '', $time = 3){
        $this->message($message, $url,$time);
    }

    public function success($message, $url = '', $time = 3){
        $this->message($message, $url,$time);
    }

    public function input($type='get',$key=''){
        switch ($type) {
            case 'get':
                $data = remove_xss($this->_request->get($key));
                break;
            case 'post':
                $data = $this->_request->getPost($key);
                break;
            case 'param':
                $data = $this->_request->getParams();
                break;
            case 'query':
                $data = $this->_request->getQuery();
                break;
            default:
                $data = '';
                break;
        }
        $result =  $this->filter($data);
        return $result;
    }

    private function filter($array){
        if(is_string($array)){
            return remove_xss($array);
        }else{
            foreach ($array as $k => $v) {
                if (is_string($v)){
                    $array[$k] = remove_xss($v);
                } else if (is_array($v)){
                    $array[$k] = $this->filter($v);
                }
            }
        }
        return $array;
    }

    /**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type AJAX返回数据格式
     * @param int $json_option 传递给json_encode的option参数
     * @return void
     */
    protected function ajaxReturn($data,$type='',$json_option=0) {
        if(empty($type)) $type  =  'JSON' ;
        switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data,$json_option));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
            case 'JSONP':
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                $handler  =   isset($_GET['callback']) ? $_GET['callback'] : 'jsonpReturn';
                exit($handler.'('.json_encode($data,$json_option).');');  
            case 'EVAL' :
                // 返回可执行的js脚本
                header('Content-Type:text/html; charset=utf-8');
                exit($data);            
            default     :
                // 用于扩展其他返回格式数据
                Hook::listen('ajax_return',$data);
        }
    }

    /**
     * 析构方法
     * 
     */
    public function __destruct() {
        
    }

}
